package org.lingshi.shopping_manager_api.service.impl;

import cn.dev33.satoken.secure.SaSecureUtil;
import cn.dev33.satoken.stp.StpUtil;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.lingshi.common.entity.Admin;
import org.lingshi.common.result.BaseResult;
import org.lingshi.common.service.IAdminService;
import org.lingshi.shopping_manager_api.service.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

import static org.lingshi.common.exception.BusCodeEnum.LOGIN_ERROR;

@Service
public class LoginServiceImpl implements ILoginService {
    @DubboReference
    private IAdminService adminService;

    @Override
    public BaseResult login(String username, String password) {
        Admin admin = adminService.findByAdminName(username);
        if (Objects.nonNull(admin)){
            String md5Password = SaSecureUtil.md5(password);
//            System.out.println("md5="+md5Password+"  || psw= "+admin.getPassword()+" ==================>>"+md5Password.equals(admin.getPassword()));
            if (md5Password.equals(admin.getPassword())){
                StpUtil.login(username);

                String tokenValue = StpUtil.getTokenInfo().getTokenValue();

                return BaseResult.success(tokenValue);
            }
        }

        return BaseResult.error(LOGIN_ERROR.getMessage()) ;
    }
}
